package com;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FolderSize {
	public static void main(String[] args) {
		FolderSize folderSize = new FolderSize();
		File dir = new File("D:/Program Files/apache-tomcat-7.0.22/webapps/bunny@gmail.com");
		System.out.println(folderSize.getDirSizeInMegabytes(dir));
		
	}

	double getDirSize(File dir) {
		double size = 0.000;
		if (dir.isFile()) {
			size = dir.length();
		} else {
			File[] subFiles = dir.listFiles();
			if(subFiles!=null){
				for (File file : subFiles) {
					if (file.isFile()) {
						size += file.length();
					} else {
						size += this.getDirSize(file);
					}
	
				}
			}
		}
		//System.out.println(size);
		return size;
	}

	String getDirSizeInMegabytes(File dir) {
		DecimalFormat decimalFormat = new DecimalFormat("##.###");
		return decimalFormat.format(this.getDirSize(dir)/1024/1024);
		//1048576
	}
}